﻿using ManagementApi.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.System.Security;
using Modules.System.User;
using PmSoft.ApplicationContext;
using System.ComponentModel.DataAnnotations;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// 常用业务接口-项目管理员角色
    /// </summary>
    [ApiController]
    [Route("/api/v1/sysuser/common")]
    [Authorize(Roles = "SysUser")]
    public class CommonController : ControllerBase
    {
        private readonly SecurityService securityService;
        private readonly IApplicationContext applicationContext;

        /// <summary>
        /// </summary>
        /// <param name="securityService"></param>
        /// <param name="applicationContext"></param>
        public CommonController(SecurityService securityService, IApplicationContext applicationContext)
        {
            this.securityService = securityService;
            this.applicationContext = applicationContext;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="passWord"></param>
        /// <returns></returns>
        [HttpPut("updatePassWord")]
        public ApiResult UpdatePassWord([Required(ErrorMessage = "密码不能为空"), MinLength(8, ErrorMessage = "密码至少8位,必须由大小写字母+数字组合")] string passWord)
        {
            SysSecurity? sysSecurity = securityService.GetSysSecurity(applicationContext.CurrentUser?.SecurityId ?? 0);
            if (sysSecurity != null)
            {
                sysSecurity.Password = SecurityPasswordHelper.EncodePassword(passWord, UserPasswordFormat.SM4);
                securityService.Update(sysSecurity);
            }
            return ApiResult.Success("修改成功");
        }
    }
}